package com.example.yuejian.yuejian.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.yuejian.yuejian.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class ClubFragment extends Fragment implements View.OnClickListener{
    private LinearLayout mTabPop;
    private LinearLayout mTabNear;

    private LinearLayout mTabAll;

    private TextView tvPop;
    private TextView tvNear;

    private Fragment mFmPop;
    private Fragment mFmNear;
    private View view;


    public ClubFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
       view= inflater.inflate(R.layout.fragment_club, container, false);
        initViews();
        initEvents();
        setSelect(0);

        return  view;
    }
    private void initViews()
    {
        mTabPop=(LinearLayout)view.findViewById(R.id.id_tab_club_pop);
        mTabNear=(LinearLayout)view.findViewById(R.id.id_tab_club_near);


        mTabAll=(LinearLayout)view.findViewById(R.id.id_tab_club_top_all);

        tvPop=(TextView)view.findViewById(R.id.tv_tab_club_pop);
        tvNear=(TextView)view.findViewById(R.id.tv_tab_club_near);

    }
    private void initEvents(){
        mTabPop.setOnClickListener(this);
        mTabNear.setOnClickListener(this);

    }

    private void resetTextColors() {

        tvPop.setTextColor(ContextCompat.getColor(getActivity(), R.color.white));
        tvNear.setTextColor(ContextCompat.getColor(getActivity(), R.color.white));

    }
    private  void resetBgColors(){
       // mTabPop.setBackgroundResource(R.drawable.shape_home_top);
       // mTabNear.setBackgroundResource(R.drawable.shape_home_top);
       mTabPop.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.gray));
        mTabNear.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.gray));
        //mTabUserphoto.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.white));


    }

    @Override
    public void onClick(View v) {

        resetTextColors();
        resetBgColors();
        switch (v.getId())
        {
            case R.id.id_tab_club_pop:
                setSelect(0);
                break;
            case R.id.id_tab_club_near:
                setSelect(1);
                break;
            default:
                break;
        }
    }
    private void setSelect(int i) {
        FragmentManager fm =getChildFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);

        switch (i)
        {
            case 0:
                if (mFmPop== null)
                {
                    mFmPop = new ClubPopFragment();
                    transaction.add(R.id.id_content_club, mFmPop);
                } else
                {
                    transaction.show(mFmPop);
                }
                tvPop.setTextColor(ContextCompat.getColor(getActivity(), R.color.gray));
                 mTabPop.setBackgroundResource(R.drawable.shape_home_top);
                //mTabPop.setBackgroundColor(ContextCompat.getColor(getActivity(), R.drawable.shape_home_top));

                break;
            case 1:
                if (mFmNear == null)
                {
                    mFmNear = new ClubNearFragment();
                    transaction.add(R.id.id_content_club, mFmNear);
                } else
                {
                    transaction.show(mFmNear);

                }

                tvNear.setTextColor(ContextCompat.getColor(getActivity(), R.color.gray));
                //mTabNear.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.gray));
               mTabNear.setBackgroundResource(R.drawable.shape_home_top);
                break;

            default:
                break;
        }

        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction) {
        if (mFmPop != null)
        {
            transaction.hide(mFmPop);
        }
        if (mFmNear != null)
        {
            transaction.hide(mFmNear);
        }

    }

}
